package com.six.clinic.common.custom_enum;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: shinan
 * @Version: 1.8
 * @create: 2020/11/30 20:13
 * @Description: 门诊类型
 */
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@Getter
@AllArgsConstructor
public enum ClinicType{

    /**
     * 初诊
     */
    FIRST_VISIT(0, "初诊"),

    /**
     * 复诊
     */
    SUBSEQUENT_VISIT(1, "复诊");

    private final Integer value;
    private final String clinicType;

    /**
     * 用于保存所有的枚举值
     */
    private static Map<String, ClinicType> RESOURCE_MAP = Stream
            .of(ClinicType.values())
            .collect(Collectors.toMap(s -> s.getClinicType(), Function.identity()));

    /**
     * 枚举反序列话调用该方法
     * @param jsonNode
     * @return
     */
    @JsonCreator
    public static ClinicType des(final JsonNode jsonNode) {
        return Optional
                .ofNullable(RESOURCE_MAP.get(jsonNode.get("clinicType").asText()))
                .orElseThrow(() -> new IllegalArgumentException(jsonNode.get("clinicType").asText()));
    }

    public static ClinicType getEnum(Integer value) {
        for (ClinicType clinicType : ClinicType.values()) {
            if (clinicType.getValue().equals(value)) {
                return clinicType;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return String.valueOf(this.value);
    }
}